package m.jclecai.com.ui;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;


import java.io.IOException;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.OnClick;
import m.jclecai.com.MyApplication;
import m.jclecai.com.R;
import m.jclecai.com.adapter.LotteryDetailsAdapter;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.OpenGradeBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.inter.OnResultCallback;
import m.jclecai.com.utils.OkHttpClientManager;
import m.jclecai.com.utils.ToastUtil;
import m.jclecai.com.utils.http.FastOk;

/**
 * Created by hp on 2017/3/14.
 * 开奖详情
 */

public class LotteryDetailsActivity extends BaseActivity {

    @BindView(R.id.tv_date)
    TextView tv_date;
    @BindView(R.id.tv_totalSales)
    TextView tv_totalSales; //总销量
    @BindView(R.id.tv_bonusPool)
    TextView tv_bonusPool; //奖池
    @BindView(R.id.lv_lotteryDetails)
    ListView lv_lotteryDetails;
    @BindViews({R.id.tv_ball_one,R.id.tv_ball_two,R.id.tv_ball_three,
            R.id.tv_ball_four,R.id.tv_ball_five,R.id.tv_ball_six,
            R.id.tv_ball_seven,R.id.tv_ball_eight})
    TextView[] texts;
    LotteryDetailsAdapter adapter;
    String type;
    @BindView(R.id.ll_summarize)
    LinearLayout ll_summarize;
    @BindView(R.id.bt_bottom_left)
    Button bt_bottom_left;
    @BindView(R.id.bt_bottom_right)
    Button bt_bottom_right;

    @Override
    protected int getContentView() {
        return R.layout.lottery_details_activity;
    }


    @Override
    protected void initView() {
        setTitle("开奖详情");
        leftOptionText("");
        rightOptionText("开奖历史");
        adapter = new LotteryDetailsAdapter();
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        String date = intent.getStringExtra("date");
        String term = intent.getStringExtra("term");
        String typeName = intent.getStringExtra("typeName");
        setTitle(typeName);
        type = intent.getStringExtra("type");

        tv_date.setText("第" + term + "期" + "　" + date);
        getOpenGradeData(term, type);

        if (MyApplication.status==1){
            bt_bottom_left.setVisibility(View.GONE);
            bt_bottom_right.setVisibility(View.GONE);
            ll_summarize.setVisibility(View.GONE);
            return;
        }else {
            bt_bottom_left.setVisibility(View.VISIBLE);
        }

        if (type.equals("ssq")) {
            bt_bottom_left.setText("双色球投注");
        } else if (type.equals("dlt")) {
            bt_bottom_left.setText("大乐透投注");
        } else if (type.equals("sd")) {
            bt_bottom_left.setText("福彩3D投注");
            bt_bottom_right.setVisibility(View.GONE);
            ll_summarize.setVisibility(View.GONE);
        } else if (type.equals("cqssc")) {
            bt_bottom_left.setText("时时彩投注");
            bt_bottom_right.setVisibility(View.GONE);
        } else if (type.equals("qlc")) {
            bt_bottom_left.setText("七乐彩投注");
            bt_bottom_right.setVisibility(View.GONE);
        } else if (type.equals("qxc")) {
            bt_bottom_left.setText("七星彩投注");
            bt_bottom_right.setVisibility(View.GONE);
        } else if (type.equals("plw")) {
            bt_bottom_left.setText("排列五投注");
            bt_bottom_right.setVisibility(View.GONE);
            ll_summarize.setVisibility(View.GONE);
        } else if (type.equals("pls")) {
            bt_bottom_left.setText("排列三投注");
            bt_bottom_right.setVisibility(View.GONE);
            ll_summarize.setVisibility(View.GONE);
        } else if (type.equals("sdsyxw")) {
            bt_bottom_left.setText("山东11选5投注");
            bt_bottom_right.setVisibility(View.GONE);
        } else if (type.equals("syxw")) {
            bt_bottom_left.setText("江西11选5投注");
            bt_bottom_right.setVisibility(View.GONE);
        }


    }


    private void getOpenGradeData(String term, String type) {
        showDialog();
        OkHttpClientManager.Param url = new OkHttpClientManager.Param("url", Constant_Url.OPEN_GRADE);
        OkHttpClientManager.Param Term = new OkHttpClientManager.Param("term", term);
        OkHttpClientManager.Param Type = new OkHttpClientManager.Param("type", type);
        OkHttpClientManager.Param[] param = new OkHttpClientManager.Param[]{url, Term, Type};
        try {
            OkHttpClientManager.getInstance().asynPost(new OnResultCallback() {
                @Override
                public void onGetResult(String json) {
                    dissDialog();
                    OpenGradeBean bean = FastOk.get(json, OpenGradeBean.class);
                    setViewInfo(bean);
                }

                @Override
                public void onError() {
                    dissDialog();
                    ToastUtil.showToast(LotteryDetailsActivity.this, R.string.connect_failure);
                }
            }, param);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    int length; //蓝球加红球长度

    private void setViewInfo(OpenGradeBean bean) {
        if (bean == null) return;
        String codes = bean.getCodes();
        if (!TextUtils.isEmpty(codes)) {
            if (type.equals("ssq") || type.equals("dlt") || type.equals("qlc")) {
                setContainBuleBallView(codes, type);
            } else {
                setNullBuleBallView(codes);
            }
        }
        tv_totalSales.setText(bean.getAllMoney());
        tv_bonusPool.setText(bean.getNextPrize());

        adapter.setData(this, bean.getPrizeLevel());
        lv_lotteryDetails.setAdapter(adapter);

    }

    int add;

    //设置包含蓝球的View
    private void setContainBuleBallView(String codes, String type) {
        String code = null;
        if (type.equals("dlt")) {
            add = 1;
        } else {
            add = 0;
        }
        String[] split = codes.split("\\|");
        code = split[0] + ","+split[1];
        String[] redBall = code.split(",");
        for (int i = 0; i < redBall.length; i++) {
            if (i < redBall.length - 1 - add) {
                texts[i].setBackgroundResource(R.drawable.red_ball);
            } else {
                texts[i].setBackgroundResource(R.drawable.blue_ball);
            }
            texts[i].setText(redBall[i]);
        }
        for (int i = 0; i < texts.length; i++) {
            if (i < redBall.length) {
                texts[i].setVisibility(View.VISIBLE);
            } else {
                texts[i].setVisibility(View.GONE);
            }
        }
    }

    private void setNullBuleBallView(String codes) {
        String[] redBall = codes.split(",");
        for (int i = 0; i < redBall.length; i++) {
            texts[i].setBackgroundResource(R.drawable.red_ball);
            texts[i].setText(redBall[i]);
        }
        for (int i = 0; i < texts.length; i++) {
            if (i < redBall.length) {
                texts[i].setVisibility(View.VISIBLE);
            } else {
                texts[i].setVisibility(View.GONE);
            }
        }
    }

    @OnClick({R.id.tv_right_option, R.id.bt_bottom_left,R.id.bt_bottom_right})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_right_option:
                Intent intent = new Intent(this, LotteryHistoryActivity.class);
                intent.putExtra("type", type);
                startActivity(intent);
                break;
            case R.id.bt_bottom_left:
                if (type.equals("ssq") || type.equals("dlt") || type.equals("qlc")) {
                    intent = new Intent(this, BetSSQ_DLT_QLCActivity.class);
                    intent.putExtra("type", type);
                    startActivity(intent);
                } else if (type.equals("pls") || type.equals("plw") || type.equals("qxc") || type.equals("sd")) {
                    intent = new Intent(this, BetPlS_PLW_SD_QXCActivity.class);
                    intent.putExtra("type", type);
                    startActivity(intent);
                } else if (type.equals("sdsyxw") || type.equals("syxw")) {
                    intent = new Intent(this, BetSYXWActivity.class);
                    intent.putExtra("type", type);
                    startActivity(intent);
                } else if (type.equals("sfc") || type.equals("rxj")) {
                    intent = new Intent(this, BetRXJ_SFCActivity.class);
                    intent.putExtra("type", type);
                    startActivity(intent);
                }
                break;
            case R.id.bt_bottom_right:
                break;
        }
    }
}
